#include "Tank0.h"
#include "global.h"
#include "SimpleAudioEngine.h"

void Tank0::update(float dt) {

    the = this->getPosition();
    //CCLOG("tank0 update %f %f",this->getContentSize().width,this->getContentSize().height);
	CCArray * layerChildren = global_instance->bonus->getChildren();
	if(layerChildren==NULL) return;
	for (int i = 0; i < layerChildren->count(); i++) {
		CCSprite * selChild =(CCSprite *) layerChildren->objectAtIndex(i);
		if (selChild != NULL) {
			//if (l.get(i) instanceof Bonus) {
			Bonus* bon = (Bonus* ) selChild;// (Tools.instance.bonus.members[i]
			// as Object).data;
			
			if (global_overlapsSprite(this, bon)) {

				BonusScore *bons = new BonusScore(500, bon->getPosition().x, bon->getPosition().y);bons->autorelease();

				global_instance->grass->addChild(bons);

				if (global_sound) {
                    //todo SoundEngine.playEffect(Resource.s_buttonEffect);
                    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("SoundEat.mp3");
                }
                map_levelScore[3]+=1;
				switch (bon->type) {
				case 5:
					shield = new Shield();shield->autorelease();
					shield->setPosition(ccp(13,13));
					this->addChild(shield);
					break;
				case 1:
					global_run = false;
					stopTank = 1;
					break;
				case 0:
					map_life++;global_instance->showLife();
					break;
				case 3:
					for (int j = 0; j < map_tankArr->count(); j++) {
						Tank* tank =(Tank *) map_tankArr->objectAtIndex(j);
						if (tank->theid != 0) {
							tank->destory();
						}
					}
					map_tankArr = new CCArray();
					map_tankArr->addObject(global_myTank);
					break;
				case 4:
					global_shotLevel -= .1;
					if (global_shotLevel < .1)
						global_shotLevel = .1;
					break;
				case 2:
					global_instance->repair();
					break;
				default:
					break;

				}
				bon->getParent()->removeChild(bon,true);
			}
		}
	}

	if (stopTank < 501)
		stopTank++;

	if (stopTank == 500) {
		global_run = true;
		stopTank = 501;
	}

}

void Tank0::move(float dt,int dir) {
	//the = this->getPosition();
	switch (dir) {
	case 1: {
		tryMoveUp(dt*speed);
		break;
			}
	case 2: {
		tryMoveDown(dt*speed);
		break;
			}
	case 3: {
		tryMoveLeft(dt*speed);
		break;
			}
	case 4: {
		tryMoveRight(dt*speed);
		break;
			}
	default:
		break;
	}
}

Tank0::Tank0(float x, float y):Tank("player.png",x,y)
{
    time = 30;
    stopTank = 501;
    isMe = true;
    speed = 1.7*60;
    theid = 0;
    life = 1;
    score = 0;
    //global_tank0num = 1;
    stopTank = 501;
    shield = new Shield();shield->autorelease();
    shield->setPosition(ccp(11,11));
    global_tank0god=true;
   
    addChild(shield);

	//this->scheduleUpdate();
}


